<?php
namespace app\api\controller;
require_once '../extend/lib/WxPayDataBase.php';
use think\Controller;
use think\Db;
use lib\WxPayApi;
use lib\WxPayException;
use lib\WxPayConfigInterface;
use lib\WxPayDataBase;
use lib\WxPayUnifiedOrder;
use lib\WxPayNotify;
use lib\WxPayConfig;
use lib\WeixinPay;
class Pay extends Base{

    private $key;
    //下单->支付
    public function orderin(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 502, 'msg' => '请给openid']);exit();
        }
        if(!empty(input("param.p_id"))){
            $p_id=input("param.p_id");
        }else{
            return json(['code' => 503, 'msg' => '请给产品id']);exit();
        }
        if(!empty(input("param.formid"))){
            $formid=input("param.formid");
        }else{
            return json(['code' => 504, 'msg' => '请给formid']);exit();
        }
        if(!empty(input("param.address_id"))){
            $address_id=input("param.address_id");
        }else{
            return json(['code' => 505, 'msg' => '请给地址id']);exit();
        }
        if(!empty(input("param.price"))){
            $price=input("param.price");
        }else{
            return json(['code' => 506, 'msg' => '请给单价']);exit();
        }
        if(!empty(input("param.buynum"))){
            $buynum=input("param.buynum");
        }else{
            return json(['code' => 507, 'msg' => '请给购买数量']);exit();
        }
        if(!empty(input("param.payfee"))){
            $payfee=input("param.payfee");
        }else{
            return json(['code' => 508, 'msg' => '请给支付总价']);exit();
        }
        if(!empty(input("param.parm"))){
            $parm=input("param.parm");
        }else{
            return json(['code' => 509, 'msg' => '请给产品参数']);exit();
        }
        // if(!empty(input("param.expressfee"))){
            $expressfee=input("param.expressfee");
        // }else{
        //     return json(['code' => 513, 'msg' => '请给快递费']);exit();
        // }
        if(!empty(input("param.allpros"))){
            $allpros=input("param.allpros");
        }else{
            return json(['code' => 514, 'msg' => '请给订单数组']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                if(count($allpros)>1){
                    for ($i=0; $i <count($allpros) ; $i++) { 
                        $numbers=Db::name('shopgoods')->where('id',$allpros[$i]['id'])->value('numbers');
                        if($numbers<$allpros[$i]['buynum']){
                            return json(['code' => 512, 'msg' => $allpros[$i]['title'].'库存不足']);exit();
                        }
                    }
                    //启动事物存入数据库 
                    Db::startTrans();
                    try {
                        $checkdata = [
                            'cus_id' =>$cus_id,
                            'openid' =>$openid,
                            'p_id' =>$p_id,
                            'sub' =>2,
                            'formid' =>$formid,
                            'address_id' =>$address_id,
                            'price' => $price,
                            'buynum' => $buynum,
                            'payfee' => $payfee,
                            'expressfee' => $expressfee,
                            'paycode' => 'T'.date('YmdHis',time()).mt_rand(000,999),
                            'parm' => $parm,
                            'paystate' =>1,
                            'time' => time(),
                            'uptime' => time(),
                        ];
                        $insertid = Db::name('orders')->insertGetId($checkdata);
                        for ($o=0; $o <count($allpros) ; $o++) { 
                            $scheckdata[$o] = [
                                'cus_id' =>$cus_id,
                                'openid' =>$openid,
                                'p_id' =>$allpros[$o]['id'],
                                'of_id' =>$insertid,
                                'formid' =>$formid,
                                'address_id' =>$address_id,
                                'price' => $allpros[$o]['price'],
                                'buynum' => $allpros[$o]['buynum'],
                                'payfee' => $payfee,
                                'expressfee' => $expressfee,
                                'paycode' => $checkdata['paycode'],
                                'parm' => $allpros[$o]['parms'],
                                'paystate' =>1,
                                'time' => time(),
                                'uptime' => time(),
                            ];
                            Db::name('ordersub')->insert($scheckdata[$o]);
                            Db::name('shopgoods')->where('id',$allpros[$o]['id'])->setDec('numbers',$allpros[$o]['buynum']);
                        }
                        if(input("param.detailtype")==2){
                            for ($c=0; $c <count($allpros) ; $c++) { 
                                Db::name('shopcart')->where('id',$allpros[$c]['cart_id'])->update(['state'=>3]);
                            }
                        }
                        // 提交事务
                        Db::commit();
                        if($insertid){
                            return json(['code' => 200, 'msg' => '成功！', 'order_id' =>$insertid, 'paycode' =>$checkdata['paycode']]);exit();
                        }else{
                            return json(['code' => 500, 'msg' => '失败！']);exit();
                        }
                    } catch (\Exception $e) {
                        // 回滚事务
                        Db::rollback();
                        return json(['code' => 500, 'msg' => '失败！']);exit();
                    }
                }else{
                    $numbers=Db::name('shopgoods')->where('id',$p_id)->value('numbers');
                    if($numbers>=$buynum){
                        $isorder=Db::name('orders')->where('cus_id',$cus_id)->where('openid',$openid)->where('p_id',$p_id)->where('paystate',1)->find();
                        if($isorder){
                            //启动事物存入数据库 
                            Db::startTrans();
                            try {
                                Db::name('shopgoods')->where('id',$p_id)->setDec('numbers',$buynum);
                                $checkdata = [
                                    'formid' =>$formid,
                                    'address_id' =>$address_id,
                                    'price' => $price,
                                    'buynum' => $buynum,
                                    'payfee' => $payfee,
                                    'expressfee' => $expressfee,
                                    'paycode' => 'T'.date('YmdHis',time()).mt_rand(000,999),
                                    'parm' => $parm,
                                    'uptime' => time(),
                                ];
                                Db::name('orders')->where('id',$isorder['id'])->update($checkdata);
                                if(input("param.detailtype")==2){
                                    for ($c=0; $c <count($allpros) ; $c++) { 
                                        Db::name('shopcart')->where('id',$allpros[$c]['cart_id'])->update(['state'=>3]);
                                    }
                                }
                                // 提交事务
                                Db::commit();
                                return json(['code' => 200, 'msg' => '成功！', 'order_id' =>$isorder['id'], 'paycode' =>$checkdata['paycode']]);exit();
                            } catch (\Exception $e) {
                                // 回滚事务
                                Db::rollback();
                                return json(['code' => 500, 'msg' => '失败！']);exit();
                            }
                        }else{
                            //启动事物存入数据库 
                            Db::startTrans();
                            try {
                                Db::name('shopgoods')->where('id',$p_id)->setDec('numbers',$buynum);
                                $checkdata = [
                                    'cus_id' =>$cus_id,
                                    'openid' =>$openid,
                                    'p_id' =>$p_id,
                                    'formid' =>$formid,
                                    'address_id' =>$address_id,
                                    'price' => $price,
                                    'buynum' => $buynum,
                                    'payfee' => $payfee,
                                    'expressfee' => $expressfee,
                                    'paycode' => 'T'.date('YmdHis',time()).mt_rand(000,999),
                                    'parm' => $parm,
                                    'paystate' =>1,
                                    'time' => time(),
                                    'uptime' => time(),
                                ];
                                $insertid = Db::name('orders')->insertGetId($checkdata);
                                if(input("param.detailtype")==2){
                                    for ($c=0; $c <count($allpros) ; $c++) { 
                                        Db::name('shopcart')->where('id',$allpros[$c]['cart_id'])->update(['state'=>3]);
                                    }
                                }
                                // 提交事务
                                Db::commit();
                                if($insertid){
                                    return json(['code' => 200, 'msg' => '成功！', 'order_id' =>$insertid, 'paycode' =>$checkdata['paycode']]);exit();
                                }else{
                                    return json(['code' => 500, 'msg' => '失败！']);exit();
                                }
                            } catch (\Exception $e) {
                                // 回滚事务
                                Db::rollback();
                                return json(['code' => 500, 'msg' => '失败！']);exit();
                            }
                        }
                    }else{
                        return json(['code' => 512, 'msg' => '库存不足！']);exit();
                    }
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                if(count($allpros)>1){
                    for ($i=0; $i <count($allpros) ; $i++) { 
                        $numbers=Db::name('shopgoods')->where('id',$allpros[$i]['id'])->value('numbers');
                        if($numbers<$allpros[$i]['buynum']){
                            return json(['code' => 512, 'msg' => $allpros[$i]['title'].'库存不足']);exit();
                        }
                    }
                    //启动事物存入数据库 
                    Db::startTrans();
                    try {
                        $checkdata = [
                            'cus_id' =>$cus_id,
                            'openid' =>$openid,
                            'p_id' =>$p_id,
                            'sub' =>2,
                            'formid' =>$formid,
                            'address_id' =>$address_id,
                            'price' => $price,
                            'buynum' => $buynum,
                            'payfee' => $payfee,
                            'expressfee' => $expressfee,
                            'paycode' => 'T'.date('YmdHis',time()).mt_rand(000,999),
                            'parm' => $parm,
                            'paystate' =>1,
                            'time' => time(),
                            'uptime' => time(),
                        ];
                        $insertid = Db::name('orders')->insertGetId($checkdata);
                        for ($o=0; $o <count($allpros) ; $o++) { 
                            $scheckdata[$o] = [
                                'cus_id' =>$cus_id,
                                'openid' =>$openid,
                                'p_id' =>$allpros[$o]['id'],
                                'of_id' =>$insertid,
                                'formid' =>$formid,
                                'address_id' =>$address_id,
                                'price' => $allpros[$o]['price'],
                                'buynum' => $allpros[$o]['buynum'],
                                'payfee' => $payfee,
                                'expressfee' => $expressfee,
                                'paycode' => $checkdata['paycode'],
                                'parm' => $allpros[$o]['parms'],
                                'paystate' =>1,
                                'time' => time(),
                                'uptime' => time(),
                            ];
                            Db::name('ordersub')->insert($scheckdata[$o]);
                            Db::name('shopgoods')->where('id',$allpros[$o]['id'])->setDec('numbers',$allpros[$o]['buynum']);
                        }
                        if(input("param.detailtype")==2){
                            for ($c=0; $c <count($allpros) ; $c++) { 
                                Db::name('shopcart')->where('id',$allpros[$c]['cart_id'])->update(['state'=>3]);
                            }
                        }
                        // 提交事务
                        Db::commit();
                        if($insertid){
                            return json(['code' => 200, 'msg' => '成功！', 'order_id' =>$insertid, 'paycode' =>$checkdata['paycode']]);exit();
                        }else{
                            return json(['code' => 500, 'msg' => '失败！']);exit();
                        }
                    } catch (\Exception $e) {
                        // 回滚事务
                        Db::rollback();
                        return json(['code' => 500, 'msg' => '失败！']);exit();
                    }
                }else{
                    $numbers=Db::name('shopgoods')->where('id',$p_id)->value('numbers');
                    if($numbers>=$buynum){
                        $isorder=Db::name('orders')->where('cus_id',$cus_id)->where('openid',$openid)->where('p_id',$p_id)->where('paystate',1)->find();
                        if($isorder){
                            //启动事物存入数据库 
                            Db::startTrans();
                            try {
                                Db::name('shopgoods')->where('id',$p_id)->setDec('numbers',$buynum);
                                $checkdata = [
                                    'formid' =>$formid,
                                    'address_id' =>$address_id,
                                    'price' => $price,
                                    'buynum' => $buynum,
                                    'payfee' => $payfee,
                                    'expressfee' => $expressfee,
                                    'paycode' => 'T'.date('YmdHis',time()).mt_rand(000,999),
                                    'parm' => $parm,
                                    'uptime' => time(),
                                ];
                                Db::name('orders')->where('id',$isorder['id'])->update($checkdata);

                                if(input("param.detailtype")==2){
                                    for ($c=0; $c <count($allpros) ; $c++) { 
                                        Db::name('shopcart')->where('id',$allpros[$c]['cart_id'])->update(['state'=>3]);
                                    }
                                }
                                // 提交事务
                                Db::commit();
                                return json(['code' => 200, 'msg' => '成功！', 'order_id' =>$isorder['id'], 'paycode' =>$checkdata['paycode']]);exit();
                            } catch (\Exception $e) {
                                // 回滚事务
                                Db::rollback();
                                return json(['code' => 500, 'msg' => '失败！']);exit();
                            }
                        }else{
                            //启动事物存入数据库 
                            Db::startTrans();
                            try {
                                Db::name('shopgoods')->where('id',$p_id)->setDec('numbers',$buynum);
                                $checkdata = [
                                    'cus_id' =>$cus_id,
                                    'openid' =>$openid,
                                    'p_id' =>$p_id,
                                    'formid' =>$formid,
                                    'address_id' =>$address_id,
                                    'price' => $price,
                                    'buynum' => $buynum,
                                    'payfee' => $payfee,
                                    'expressfee' => $expressfee,
                                    'paycode' => 'T'.date('YmdHis',time()).mt_rand(000,999),
                                    'parm' => $parm,
                                    'paystate' =>1,
                                    'time' => time(),
                                    'uptime' => time(),
                                ];
                                $insertid = Db::name('orders')->insertGetId($checkdata);
                                
                                if(input("param.detailtype")==2){
                                    for ($c=0; $c <count($allpros) ; $c++) { 
                                        Db::name('shopcart')->where('id',$allpros[$c]['cart_id'])->update(['state'=>3]);
                                    }
                                }
                                // 提交事务
                                Db::commit();
                                if($insertid){
                                    return json(['code' => 200, 'msg' => '成功！', 'order_id' =>$insertid, 'paycode' =>$checkdata['paycode']]);exit();
                                }else{
                                    return json(['code' => 500, 'msg' => '失败！']);exit();
                                }
                            } catch (\Exception $e) {
                                // 回滚事务
                                Db::rollback();
                                return json(['code' => 500, 'msg' => '失败！']);exit();
                            }
                        }
                    }else{
                        return json(['code' => 512, 'msg' => '库存不足！']);exit();
                    }
                }
            }else{
                return json(['code' => 511, 'msg' => '未开启此平台！']);exit();
            }
        }else{
            return json(['code' => 510, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //下单立即支付
    public function tmdpay(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $p_id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给出产品id']);exit();
        }
        if(!empty(input("param.order_id"))){
            $order_id=input("param.order_id");
        }else{
            return json(['code' => 504, 'msg' => '请给订单id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 505, 'msg' => '请给出下单者的openid']);exit();
        }
        if(!empty(input("param.paycode"))){
            $paycode=input("param.paycode");
        }else{
            return json(['code' => 506, 'msg' => '请给出订单号']);exit();
        }
        if(!empty(input("param.payfee"))){
            $payfee=input("param.payfee");
        }else{
            return json(['code' => 507, 'msg' => '交易金额呢']);exit();
        }
        $aboutus=Db::name('aboutus')->where('cus_id',$cus_id)->find();
        $key=$aboutus['apikey'];
        if($payfee>0)
        {
            $body = "产品订单";
            $total_fee = floatval($payfee*100);
        }
        $config = new WxPayUnifiedOrder();
        $config->SetAppid($aboutus['appid']);
        $config->SetMch_id($aboutus['mac_id']);
        $config->SetMerchantId($aboutus['mac_id']);
        $config->SetSignType('MD5');
        $config->SetKey($key);
        $config->SetProxy("0.0.0.0",0);
        $config->SetReportLevenl(1);
        $config->SetTimeStamp(time());
        $config->SetPackage('prepay_id=');
        $config->SetNotify_url(config('tmdhttp').$_SERVER['HTTP_HOST'].'/api/pay/tmdnotify');

        $inputObj = new WxPayUnifiedOrder();
        $inputObj->SetBody($body);
        $inputObj->SetOut_trade_no($paycode);
        $inputObj->SetTotal_fee($total_fee);
        $inputObj->SetNotify_url(config('tmdhttp').$_SERVER['HTTP_HOST'].'/api/pay/tmdnotify');
        $inputObj->SetTrade_type("JSAPI");
        $inputObj->SetOpenid($openid);
        $inputObj->SetProduct_id($p_id);

        $weixinpay=new WxPayApi();
        $return=$weixinpay->unifiedOrder($config,$inputObj);
        $this->key=$key;
        $parameters = array(
            'appId' => $return['appid'], //小程序ID
            'timeStamp' => '' . time() . '', //时间戳
            'nonceStr' => $return['nonce_str'], //随机串
            'package' => 'prepay_id=' . $return['prepay_id'], //数据包
            'signType' => 'MD5'//签名方式
        );
        //签名
        $parameters['paySign'] = $this->getSign($parameters);
        return json(['code' => 200, 'msg' => '成功！','data'=>$parameters]);exit();
    }
    //下单后在订单里支付
    public function orderpay(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.p_id"))){
            $p_id=input("param.p_id");
        }else{
            return json(['code' => 503, 'msg' => '请给出产品id']);exit();
        }
        if(!empty(input("param.order_id"))){
            $order_id=input("param.order_id");
        }else{
            return json(['code' => 504, 'msg' => '请给订单id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 505, 'msg' => '请给出下单者的openid']);exit();
        }
        if(!empty(input("param.paycode"))){
            $paycode=input("param.paycode");
        }else{
            return json(['code' => 506, 'msg' => '请给出订单号']);exit();
        }
        if(!empty(input("param.payfee"))){
            $payfee=input("param.payfee");
        }else{
            return json(['code' => 507, 'msg' => '交易金额呢']);exit();
        }
        // if(!empty(input("param.buynum"))){
        //     $buynum=input("param.buynum");
        // }else{
        //     return json(['code' => 508, 'msg' => '购买量']);exit();
        // }
        $orderxx=Db::name('orders')->where('id',$order_id)->find();
        if($orderxx['sub']==1){
            $numbers=Db::name('shopgoods')->where('id',$p_id)->value('numbers');
            if($numbers>=$orderxx['buynum']){
                $res=Db::name('shopgoods')->where('id',$p_id)->setDec('numbers',$orderxx['buynum']);
                if($res){
                    $aboutus=Db::name('aboutus')->where('cus_id',$cus_id)->find();
                    $key=$aboutus['apikey'];
                    if($payfee>0)
                    {
                        $body = "产品订单";
                        $total_fee = floatval($payfee*100);
                    }
                    $config = new WxPayUnifiedOrder();
                    $config->SetAppid($aboutus['appid']);
                    $config->SetMch_id($aboutus['mac_id']);
                    $config->SetMerchantId($aboutus['mac_id']);
                    $config->SetSignType('MD5');
                    $config->SetKey($key);
                    $config->SetProxy("0.0.0.0",0);
                    $config->SetReportLevenl(1);
                    $config->SetTimeStamp(time());
                    $config->SetPackage('prepay_id=');
                    $config->SetNotify_url(config('tmdhttp').$_SERVER['HTTP_HOST'].'/api/pay/tmdnotify');

                    $inputObj = new WxPayUnifiedOrder();
                    $inputObj->SetBody($body);
                    $inputObj->SetOut_trade_no($paycode);
                    $inputObj->SetTotal_fee($total_fee);
                    $inputObj->SetNotify_url(config('tmdhttp').$_SERVER['HTTP_HOST'].'/api/pay/tmdnotify');
                    $inputObj->SetTrade_type("JSAPI");
                    $inputObj->SetOpenid($openid);
                    $inputObj->SetProduct_id($p_id);

                    $weixinpay=new WxPayApi();
                    $return=$weixinpay->unifiedOrder($config,$inputObj);
                    $this->key=$key;
                    $parameters = array(
                        'appId' => $return['appid'], //小程序ID
                        'timeStamp' => '' . time() . '', //时间戳
                        'nonceStr' => $return['nonce_str'], //随机串
                        'package' => 'prepay_id=' . $return['prepay_id'], //数据包
                        'signType' => 'MD5'//签名方式
                    );
                    //签名
                    $parameters['paySign'] = $this->getSign($parameters);
                    return json(['code' => 200, 'msg' => '成功！','data'=>$parameters]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '锁定库存失败']);exit();
                }
            }else{
                return json(['code' => 502, 'msg' => '库存不足！']);exit();
            }
        }elseif($orderxx['sub']==2){
            $allorder=Db::name('ordersub')->where('of_id',$order_id)->select();
            if(count($allorder)>1){
                for ($i=0; $i <count($allorder) ; $i++) { 
                    $shopgoods[$i]=Db::name('shopgoods')->where('id',$allorder[$i]['p_id'])->find();
                    if($shopgoods[$i]['numbers']<$allorder[$i]['buynum']){
                        return json(['code' => 502, 'msg' => $shopgoods[$i]['title'].'库存不足！']);exit();
                    }
                }
            }
            for ($i=0; $i <count($allorder) ; $i++) { 
                Db::name('shopgoods')->where('id',$allorder[$i]['p_id'])->setDec('numbers',$allorder[$i]['buynum']);
            }
            $aboutus=Db::name('aboutus')->where('cus_id',$cus_id)->find();
            $key=$aboutus['apikey'];
            if($payfee>0)
            {
                $body = "产品订单";
                $total_fee = floatval($payfee*100);
            }
            $config = new WxPayUnifiedOrder();
            $config->SetAppid($aboutus['appid']);
            $config->SetMch_id($aboutus['mac_id']);
            $config->SetMerchantId($aboutus['mac_id']);
            $config->SetSignType('MD5');
            $config->SetKey($key);
            $config->SetProxy("0.0.0.0",0);
            $config->SetReportLevenl(1);
            $config->SetTimeStamp(time());
            $config->SetPackage('prepay_id=');
            $config->SetNotify_url(config('tmdhttp').$_SERVER['HTTP_HOST'].'/api/pay/tmdnotify');

            $inputObj = new WxPayUnifiedOrder();
            $inputObj->SetBody($body);
            $inputObj->SetOut_trade_no($paycode);
            $inputObj->SetTotal_fee($total_fee);
            $inputObj->SetNotify_url(config('tmdhttp').$_SERVER['HTTP_HOST'].'/api/pay/tmdnotify');
            $inputObj->SetTrade_type("JSAPI");
            $inputObj->SetOpenid($openid);
            $inputObj->SetProduct_id($p_id);

            $weixinpay=new WxPayApi();
            $return=$weixinpay->unifiedOrder($config,$inputObj);
            $this->key=$key;
            $parameters = array(
                'appId' => $return['appid'], //小程序ID
                'timeStamp' => '' . time() . '', //时间戳
                'nonceStr' => $return['nonce_str'], //随机串
                'package' => 'prepay_id=' . $return['prepay_id'], //数据包
                'signType' => 'MD5'//签名方式
            );
            //签名
            $parameters['paySign'] = $this->getSign($parameters);
            return json(['code' => 200, 'msg' => '成功！','data'=>$parameters]);exit();
        }
        
    }
    public function tmdnotify(){
        $input = file_get_contents('php://input');
        if (!empty($input)) {
            $obj = simplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA);
            $data = json_decode(json_encode($obj), true);
            if ($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS') {
                $paystate=Db::name('orders')->where('paycode',$data['out_trade_no'])->find();
                if($paystate['paystate']==1){
                    Db::name('orders')->where('paycode',$data['out_trade_no'])->update(['paystate'=>2,'uptime'=>time()]);
                }
                if($paystate['sub']==2){
                    Db::name('orderssub')->where('of_id',$paystate['id'])->update(['paystate'=>2,'uptime'=>time()]);
                }
            }
        }
    }
    //取消支付库存回原
    public function numasc(){
        if(!empty(input("param.p_id"))){
            $p_id=input("param.p_id");
        }else{
            return json(['code' => 501, 'msg' => '请给产品id']);exit();
        }
        if(!empty(input("param.buynum"))){
            $buynum=input("param.buynum");
        }else{
            return json(['code' => 502, 'msg' => '请给购买数量']);exit();
        }
        if(!empty(input("param.allpros"))){
            $allpros=input("param.allpros");
        }else{
            return json(['code' => 514, 'msg' => '请给订单数组']);exit();
        }
        if(count($allpros)>1){
            for ($i=0; $i <count($allpros) ; $i++) { 
                Db::name('shopgoods')->where('id',$allpros[$i]['id'])->setInc('numbers',$allpros[$i]['buynum']);
            }
            return json(['code' => 200, 'msg' => '成功！']);exit();
        }elseif(count($allpros)==1){
            $res=Db::name('shopgoods')->where('id',$p_id)->setInc('numbers',$buynum);
        }
        if($res){
            return json(['code' => 200, 'msg' => '成功！']);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败！']);exit();
        }
    }
    //作用：生成签名
    private function getSign($Obj) {
        foreach ($Obj as $k => $v) {
            $Parameters[$k] = $v;
        }
        //签名步骤一：按字典序排序参数
        ksort($Parameters);
        $String = $this->formatBizQueryParaMap($Parameters, false);
        //签名步骤二：在string后加入KEY
        $String = $String . "&key=" . $this->key;
        //签名步骤三：MD5加密
        $String = md5($String);
        //签名步骤四：所有字符转为大写
        $result_ = strtoupper($String);
        return $result_;
    }
    ///作用：格式化参数，签名过程需要使用
    private function formatBizQueryParaMap($paraMap, $urlencode) {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            if ($urlencode) {
                $v = urlencode($v);
            }
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar;
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }
        return $reqPar;
    }
}